<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class HashesController extends AppController {

    var $name = 'Hashes';
    var $helpers = array('Html', 'Form');
    var $uses = array('Hash','Meeting','Title','Attendance','User');
    
    
    function hash($hashstring='') {
        $this->pageTitle = __('Confirm attendance',true);
        $this->Committee->recursive=-1;
        $this->Title->recursive=-1;
        
        $hash = $this->Hash->findByHash($hashstring);
        // $d is attendance model
        $d = $this->{$hash['Hash']['model']}->findById($hash['Hash']['foreign_key']);
        
        if (!empty($this->data)) { 
            if ($this->data['Hash']['hash_type'] == 'confirm') {
                $d['Attendance']['will_attend'] = $this->data['Hash']['will_attend'];
      
			if ( $d['Attendance']['will_attend'] == '1')
                {
					$d['Attendance']['excuse']   = '';
					$d['Attendance']['rep_name'] = '';
			    }	
			    else
			    {
					// if ($this->data['Hash']['will_attend'] == '0' || $this->data['Hash']['will_attend'] == '2')
					$d['Attendance']['excuse'] = $this->data['Hash']['excuse'];
					if ($this->data['Hash']['allow_rep'] && isset($this->data['Hash']['allow_rep'])) 
						$d['Attendance']['rep_name'] = $this->data['Hash']['wakil'];
				}
               
                $d['Attendance']['confirm_date'] = date('Y-m-d H:i:s');
                if ($this->Attendance->save($d)) {
                    $this->Session->setFlash(__('Attendance confirmation has been saved', true));
                    $this->redirect(array('action'=>'mainpage','controller'=>'committees'));
                } else {
                    $this->Session->setFlash(__('Attendance confirmation could not be saved. Please, try again.', true));
                }
            }
        }
        
        if (isset($hashstring) && !empty($hashstring)) { 
            $this->set('hash',$hash);
            $this->set('d',$d);
            $this->set('committee',$this->Committee->find('first',array('conditions'=>array('Committee.id'=>$d['Meeting']['committee_id']))));
            $this->set('title1',$this->Title->find('first',array('conditions'=>array('id'=>$d['User']['title_id']))));
            $this->render($hash['Hash']['hash_type']); 
        } else {
            $this->Session->setFlash(__('Invalid action.', true));
        }
    }

    function hash1($hashstring='') {
        $this->pageTitle = __('Reset password',true);
        $this->User->recursive=-1;
        $this->Title->recursive=-1;
        
        $hash = $this->Hash->findByHash($hashstring);
        //$d is user model
        $d = $this->{$hash['Hash']['model']}->findById($hash['Hash']['foreign_key']);
        if (!empty($this->data)) { 
            if ($this->data['Hash']['newpassword'] !=$this->data['Hash']['confirmpassword']){
                $this->Session->setFlash(__('Your password does not match.', true));
            } elseif ($this->data['Hash']['newpassword'] ==$this->data['Hash']['confirmpassword']) {
                $d['User']['password'] = $this->Auth->password($this->data['Hash']['newpassword']);
                if ($this->User->save($d,false)) {
                    $hash['Hash']['limit_count']=1;
                    $this->Hash->save($hash);
                    $this->Session->setFlash(__('Your password has been changed',true));
                    $this->redirect(array('action'=>'mainpage','controller'=>'committees'));
                }
            }
        }
        
        if (isset($hashstring) && !empty($hashstring)) { 
            $this->set('hash',$hash);
            $this->set('d',$d);
            $this->set('title1',$this->Title->find('first',array('conditions'=>array('id'=>$d['User']['title_id']))));
            $this->render($hash['Hash']['hash_type']); 
        } else {
            $this->Session->setFlash(__('Invalid action.', true));
        }
    }
}

?>
